package com.zsck.pos.adapter;

import android.text.TextUtils;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chinaums.commoncheck.R;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.zsck.pos.bean.BatchBills;
import com.zsck.pos.bean.CountingEvent;
import com.zsck.pos.bean.FeeEvent;
import com.zsck.pos.bean.FeeVOListBean;
import com.zsck.pos.bean.MonthBill;
import com.zsck.pos.utils.LogUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;


public class FirstProjectAdapter extends BaseNodeProvider {

    private CheckBox firstCheck;

    public FirstProjectAdapter() {
    }


    @Override
    public int getItemViewType() {
        return 1;
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_first;
    }


    @Override
    public void convert(@NotNull BaseViewHolder baseViewHolder, BaseNode baseNode) {

        BatchBills entity = (BatchBills) baseNode;
        baseViewHolder.setText(R.id.tv_project, entity.getProjectName() + "-" + entity.getRoomName());
        String collectionObjId = entity.getCollectionObjId();
        if (TextUtils.equals("2", collectionObjId)) {
            baseViewHolder.setImageResource(R.id.iv_type_role, R.mipmap.icon_role_com);
        } else {
            baseViewHolder.setImageResource(R.id.iv_type_role, R.mipmap.icon_role_personal);
        }
        List<MonthBill> monthBillList = entity.getMonthBillList();
        firstCheck = baseViewHolder.getView(R.id.first_check);
        firstCheck.setOnCheckedChangeListener(null);
        for(int i=0 ;i<monthBillList.size();i++){
            MonthBill monthBill = monthBillList.get(i);
            if(monthBill.isChecked()){
                entity.setChose(true);
                firstCheck.setChecked(true);
                break;
            }
            if(i==monthBillList.size()-1){
                entity.setChose(false);
                firstCheck.setChecked(false);
            }
        }
        LiveEventBus.get(CountingEvent.class)
                .post(new CountingEvent());
        firstCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean check) {
                LogUtil.e("firstCheck", "onCheckedChanged" + check);
                List<MonthBill> monthBillList = entity.getMonthBillList();
                entity.setChose(check);

                if (check) {
                    List<MonthBill> newBillList = new ArrayList<>();

                    for (MonthBill ma : monthBillList) {

                        if (!ma.isChecked()) {
                            ma.setChecked(check);
                        }

                        List<FeeVOListBean> feeVOList = ma.getFeeVOList();
                        for (FeeVOListBean fe : feeVOList) {
                            if (!fe.isChecked()) {
                                fe.setChecked(true);
                                LiveEventBus.get(FeeEvent.class)
                                        .post(new FeeEvent(fe, true));
                            }
                            ma.setFeeVOList(feeVOList);
                        }
                        newBillList.add(ma);
                        getAdapter().nodeReplaceChildData(entity, newBillList);
                    }
                } else {
                    List<MonthBill> newBillList = new ArrayList<>();

                    for (MonthBill ma : monthBillList) {
                        if (ma.isChecked()) {
                            ma.setChecked(false);
                        }
                        List<FeeVOListBean> feeVOList = ma.getFeeVOList();
                        for (FeeVOListBean fe : feeVOList) {
                            if (fe.isChecked()) {
                                fe.setChecked(false);
                                LiveEventBus.get(FeeEvent.class)
                                        .post(new FeeEvent(fe, false));
                            }
                        }
                        ma.setFeeVOList(feeVOList);
                        newBillList.add(ma);
                    }
                    getAdapter().nodeReplaceChildData(entity, newBillList);
                }
                LiveEventBus.get(CountingEvent.class)
                        .post(new CountingEvent());
            }
        });


    }


    @Override
    public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
        BatchBills entity = (BatchBills) data;

        if (entity.isExpanded()) {
            getAdapter().collapse(position);
        } else {
            getAdapter().expand(position);
        }
    }


}
